package ru.dimka3210.java.reminder.utils;

import javax.swing.*;
import java.awt.*;

/**
 * User: dimka3210
 * Date: 18.05.13
 * Time: 0:02
 */
public class FrameToScreenCenter {
    JFrame frame = null;
    JDialog dialog = null;

    public FrameToScreenCenter(JFrame __frame) {
        this.frame = __frame;
        Dimension frameSize = frame.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        this.setLocation((screenSize.getWidth() / 2) - (frameSize.getWidth() / 2), (screenSize.getHeight() / 2) - (frameSize.getHeight() / 2), frame);
    }

    public FrameToScreenCenter(final JDialog __dialog) {
        EventQueue.invokeLater(new Runnable() {
            public JDialog dialog;

            @Override
            public void run() {
                this.dialog = __dialog;
                Dimension frameSize = dialog.getSize();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

                FrameToScreenCenter.setLocation((screenSize.getWidth() / 2) - (frameSize.getWidth() / 2), (screenSize.getHeight() / 2) - (frameSize.getHeight() / 2), dialog);
            }
        });
    }

    protected static void setLocation(final double x, final double y, final JFrame jFrame) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                int _x = Integer.parseInt(String.valueOf(x));
                int _y = Integer.parseInt(String.valueOf(y));
                jFrame.setLocation(_x, _y);
            }
        });
    }

    protected static void setLocation(final double x, final double y, final JDialog dialog) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                int _x = Integer.parseInt(String.valueOf(x));
                int _y = Integer.parseInt(String.valueOf(y));
                dialog.setLocation(_x, _y);
            }
        });
    }
}
